package com.megvii.koala.data.manager;

import com.megvii.corelib.util.DateTimeUtil;
import com.megvii.koala.data.bean.VisitorHistoryBean;

import java.util.Comparator;

/**
 * Created by linyue on 15/12/30.
 */
public class VisitorHistoryManager extends KoalaBeanManager<VisitorHistoryBean>
{
    private VisitorManager visitorManager;

    public VisitorHistoryManager(VisitorManager visitorManager)
    {
        super(new Comparator<VisitorHistoryBean>()
        {
            @Override
            public int compare(VisitorHistoryBean lhs, VisitorHistoryBean rhs)
            {
                return (int) (rhs.timestamp - lhs.timestamp);
            }
        });
        this.visitorManager = visitorManager;
    }

    public VisitorHistoryBean getHistoryLongLongAgo()
    {
        if (isEmpty())
        {
            return null;
        }
        else
        {
            return beanList.get(beanList.size() - 1);
        }
    }


    @Override
    protected void update(VisitorHistoryBean bean, boolean sortAndIndex)
    {
        if (bean == null)
        {
            return;
        }
        filterContent(bean);
        super.update(bean, sortAndIndex);
    }

    public void filterContent(VisitorHistoryBean history)
    {
        history.photo = urlMaker.makeUrl(history.photo);
        history.timestamp = DateTimeUtil.toMSTimestamp(history.timestamp);
        visitorManager.filterCreatureContent(history.subject);
    }
}
